package demo3;

import demo2.DataFlowBean;
import demo2.DataFlowJob;
import org.apache.commons.io.output.NullWriter;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class CleanDataJob {
        //                    目标：数据清洗
    public static void main(String[] args) throws Exception{
        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:9000");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(CleanDataJob.class);

        //3. 设置输入(读取文件）和输出(输出计算结果)的类
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径
        TextInputFormat.addInputPath(job, new Path("/mapreduce/demo3/dataclean.log"));
        TextOutputFormat.setOutputPath(job, new Path("/mapreduce/demo3/out"));

        //5. 设置mapper和reducer
        job.setMapperClass(CleanDataMapper.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        // reduceTask默认的个数是1，需要设置为0
        job.setNumReduceTasks(0);

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b);


    }

    static class CleanDataMapper extends Mapper<LongWritable, Text, Text,NullWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 创建一个conter计数器组，组名g1,计数器名称为总条数
            context.getCounter("g1","总条数").increment(1L);

            if (check(value)){ // 调用自定义check校验方法
               context.write(value,NullWritable.get());
                context.getCounter("g1","成功").increment(1L);
            }else {
                context.getCounter("g1","fail").increment(1L);
            }
        }

        private boolean check(Text value) {
            String s = value.toString();
            if (StringUtils.isBlank(s)){
                // 如果S为空串，则直接返回false
                return false;
            }

            String[] arr = s.split("\t");
            if (arr.length != 9){
                return false;
            }

            //手机号
            if (StringUtils.isBlank(arr[1]) || "null".equals(arr[1])){
                return false;
            }

            if (StringUtils.isBlank(arr[6]) || "null".equals(arr[6])){
                return false;
            }

            if (StringUtils.isBlank(arr[7]) || "null".equals(arr[7])){
                return false;
            }

            return true;
        }
    }
}
